<?php
	//check if we got a page to check, otherwise we are start-page
	if(!isset($page)) {
		if(!isset($_GET["page"])) {
			$page = "start";
		}else{
			$page = $_GET["page"];
		}
	}
	$rights = getRights($db);
	if($rights > 1) {
		if($page == 41) {
			echo "<div>";
			include("mail.php");
			echo "</div>";
			
			echo "<script id=\"contentScript\">";
			echo "var onloadFunction = function() {";
			include("mailScript.php");
			echo "};";
			echo "window.onload = onloadFunction;";
			echo "</script>";
		}else{
	
			//check if the page is a num, if it is it is a sub-page
			if(is_numeric($page)) {	
				$contentQuery = mysqli_query($db, "SELECT text, script, allowScript FROM pages WHERE id='$page'");
			}else{
				$contentQuery = mysqli_query($db, "SELECT text, script, allowScript FROM pages WHERE name='$page'");
			}
			
			if(!$contentQuery) {
				echo "Page not found";
			}else{
				$queryArray = $contentQuery->fetch_assoc();
		
				if(sizeof($queryArray) > 0) {
					if($queryArray["allowScript"]) {
						if(sizeof(trim($queryArray["text"])) > 0) {
							echo "<div>";
							eval( $queryArray["text"] );
							echo "</div>";
							echo "<script id=\"contentScript\">";
							echo "var onloadFunction = function() {";
							echo $queryArray["script"];
							echo "};";
							echo "window.onload = onloadFunction;";
							echo "</script>";
						}else{
							echo "<div>";
							echo "</div>";
							echo "<script id=\"contentScript\">";
							echo "</script>";	
						}
					}else{
						echo "<div style=\"white-space: pre-wrap\">";
						echo htmlspecialchars($queryArray["text"]);
						echo "</div>";
						echo "<script id=\"contentScript\">";
						echo "</script>";
					}
				}else{
					echo "Page not found";
				}
			}
		}
	}else{
		echo "Page not found";
	}
?>